package research.dao.hibernate;

import java.sql.Connection;
import java.sql.SQLException;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class HibernateUtil {

	private static SessionFactory sessionFactory = null;
	private static Configuration configuration = null;

	public static void Initialize (String url, String db, String name, String psw) {
		try {
			configuration = new Configuration();
			configuration.configure();
			configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
			configuration.setProperty("hibernate.connection.url", "jdbc:mysql://" + url + "/" + db);
			configuration.setProperty("hibernate.connection.username", name);
			configuration.setProperty("hibernate.connection.password", psw);
			sessionFactory = configuration.buildSessionFactory();
		} catch (Throwable ex) {
			System.err.println("Initial SessionFactory creation failed." + ex);
			throw new ExceptionInInitializerError(ex);
		}
	}

	public static SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	public static Connection getConnection() throws HibernateException, SQLException {
		return configuration.buildSettings().getConnectionProvider().getConnection();
	}

}